/*
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-16 14:54:34
 * @LastEditors: charles
 * @LastEditTime: 2021-11-16 15:17:52
 */
import {Button,Form,Input,message} from 'antd'
import axios from 'axios'
import qs from 'qs'
import { token } from '../../utils/config';
function Publish(props){
  let article = {};
  if(props.location.state){
    article = props.location.state
  }
  // 返回
  const toBackHandler = ()=>{
    props.history.goBack();
  }
  // 提交
  const onFinish = (value)=>{
    let url = "http://121.199.29.84:8002/article/saveOrUpdate"
    value = Object.assign({},article,value)
    axios.post(url,qs.stringify(value),{
      headers:{
        'Content-Type':'application/x-www-form-urlencoded',
        Authorization:token
      }
    }).then(resp =>{
      message.success(resp.data.message)
      toBackHandler();
    })
  }
  return (
    <div>
      <h2>
        {article.id?"资讯修改":"资讯发布"}
      </h2>
      <div style={{marginBottom:"1em"}}>
        <Button type="text" onClick={toBackHandler}>返回</Button>
      </div>
      <Form
        name="basic"
        initialValues={ article }
        onFinish={onFinish}
      >
        <Form.Item
          label="标题"
          name="title"
          rules={[
            {
              required: true,
              message: '请输入标题!',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="内容"
          name="content"
          rules={[
            {
              required: true,
              message: '请输入内容!',
            },
          ]}
        >
          <Input.TextArea rows={4} />
        </Form.Item>

        <Form.Item >
          <Button type="primary" htmlType="submit"> Submit </Button>
        </Form.Item>
      </Form>
    </div>
  )
}
export default Publish;